package at.fhhgb.mc.android.mfb_hgb.activity;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import at.fhhgb.mc.android.mfb_hgb.R;
import at.fhhgb.mc.android.mfb_hgb.activity.dialog.ProgressDialog;
import at.fhhgb.mc.android.mfb_hgb.dal.SelectOffer;
import at.fhhgb.mc.android.mfb_hgb.model.Offer;
import at.fhhgb.mc.android.mfb_hgb.model.TaskCaller;

public class FragmentOverview extends Fragment implements OnItemClickListener, TaskCaller, TabListener {
	private static final String LOG_TAG = "FragmentOverview";
	ListView lv;
	ArrayList<Offer> offers;
	ProgressDialog pDialog;
	private boolean isTabInit = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_overview_list, null);

		lv = (ListView) view.findViewById(R.id.details_list);

		getRemoteData();

		return view;
	}

	private void getRemoteData() {
		pDialog = new ProgressDialog(getActivity());
		pDialog.show();

		switch (getArguments().getInt("type")) {
		case 0: // Empty Bundle -> all
		case 1: // All
			new SelectOffer(this).execute(new BasicNameValuePair("onlyValid", ""));
			break;

		case 2: // Offers only
			new SelectOffer(this).execute(new BasicNameValuePair("Type", "1"), new BasicNameValuePair("onlyValid", ""));
			break;

		case 3: // Searches only
			new SelectOffer(this).execute(new BasicNameValuePair("Type", "2"), new BasicNameValuePair("onlyValid", ""));
			break;

		default:
			Log.e(LOG_TAG, "Unknown parameter: " + getArguments().getInt("type"));
		}
	}

	//
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Bundle b = new Bundle();
		b.putString("name", offers.get(position).getName());

		RequestDialogFragment r = new RequestDialogFragment();
		r.setArguments(b);
		r.show(getFragmentManager(), "request");
	}

	@Override
	public void onBackgroundTaskCompleted(Object result) {
		if (getActivity() != null) {
			offers = (ArrayList<Offer>) result;
			lv.setAdapter(new FragmentOverviewAdapter(getActivity(), R.layout.fragment_overview_listitem, offers));
			lv.setOnItemClickListener(this);
		} else {
			Log.e(LOG_TAG, "getActivity() is null!");
		}

		pDialog.dismiss();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);

		if (!((OverviewActivity) getActivity()).isDrawerOpened()) {
			inflater.inflate(R.menu.overview, menu);

			ActionBar actionBar = getActivity().getActionBar();

			actionBar.setDisplayShowTitleEnabled(true);
			actionBar.setDisplayShowHomeEnabled(true);

			// DONT TOUCH! Causes infinite loop! <.<
			if (actionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS)
				actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

			actionBar.setTitle(getResources().getStringArray(R.array.nav_items)[0]);

			if (actionBar.getTabCount() < 1) {
				// ads tabs
				Tab tab = actionBar.newTab();
				tab.setText(getResources().getString(R.string.overview_tabAll));
				tab.setTabListener(this);
				actionBar.addTab(tab);

				tab = actionBar.newTab();
				tab.setText(getResources().getString(R.string.overview_tabO));
				tab.setTabListener(this);
				actionBar.addTab(tab);

				tab = actionBar.newTab();
				tab.setText(getResources().getString(R.string.overview_tabS));
				tab.setTabListener(this);
				actionBar.addTab(tab);
			}
			
			isTabInit = true;
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setHasOptionsMenu(true);
	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
	}

	@Override
	public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
		if (isTabInit) {
			Fragment f = new FragmentOverview();
			Bundle b = new Bundle();

			switch (arg0.getPosition()) {
			case 0:
				b.putInt("type", 1);
				break;

			case 1:
				b.putInt("type", 2);
				break;

			case 2:
				b.putInt("type", 3);
				break;
			}

			f.setArguments(b);
			arg1.replace(R.id.container, f);
		}
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.overview_refresh:
			FragmentOverview f = (FragmentOverview) getFragmentManager().findFragmentById(R.id.container);
			f.getRemoteData();
			return true;

		case R.id.overview_add:
			FragmentManager fragmentManager = getActivity().getFragmentManager();
			FragmentTransaction ft = fragmentManager.beginTransaction();
			NewOfferFragment nof = new NewOfferFragment();

			ft.replace(R.id.container, nof);
			ft.commit();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
}
